{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "774ce905",
   "metadata": {},
   "source": [
    "本代码是生成训练集和测试集\n",
    "\n",
    "输入：2019年第3季度的数据\n",
    "\n",
    "输出：入模训练集（分箱后） 入模测试集（分箱后）\n",
    "\n",
    "     入模训练集（woe编码后） 入模测试集（woe编码后）"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7d58794a",
   "metadata": {},
   "source": [
    "# 读取数据和引入模块"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "787b68da",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-06-22T01:51:55.155026Z",
     "start_time": "2022-06-22T01:51:53.587896Z"
    }
   },
   "outputs": [],
   "source": [
    "from sklearn.linear_model import LogisticRegression\n",
    "from sklearn.model_selection import train_test_split\n",
    "import sklearn as sk\n",
    "import pandas as pd\n",
    "import variable_bin_methods as vb\n",
    "import toad\n",
    "import variable_encode as ve\n",
    "import numpy as np\n",
    "from sklearn.metrics import roc_curve, auc\n",
    "import main\n",
    "import pickle\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "c64f838f",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-06-22T01:51:57.747245Z",
     "start_time": "2022-06-22T01:51:55.157029Z"
    }
   },
   "outputs": [],
   "source": [
    "data0=pd.read_csv('LoanStats_securev1_2019Q3.csv',low_memory=False,header=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "75af72cb",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-06-22T01:51:57.762734Z",
     "start_time": "2022-06-22T01:51:57.749248Z"
    }
   },
   "outputs": [],
   "source": [
    "data0 = data0.iloc[0:-2,:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "138937c8",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-06-22T01:51:57.777759Z",
     "start_time": "2022-06-22T01:51:57.765738Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(143035, 150)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data0.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "9107dc1f",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-06-22T01:51:58.140582Z",
     "start_time": "2022-06-22T01:51:57.778753Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    23793\n",
       "1     9099\n",
       "Name: loan_status, dtype: int64"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#data00=data0[~data0['loan_status'].str.contains('Current')]\n",
    "#data=data00.copy()\n",
    "#data.replace({'Charged Off':1,'Default':1,'Late (16-30 days)':1,'Late (31-120 days)':1,'In Grace Period':1,'Fully Paid':0},inplace=True)\n",
    "#data_y=data['loan_status']\n",
    "#data_x=data.drop('loan_status',axis=1)\n",
    "#data_y.value_counts()\n",
    "data00=data0[~data0['loan_status'].str.contains('Current')]\n",
    "data=data00.copy()\n",
    "data.replace({'Charged Off':1,'Default':1,'Late (16-30 days)':1,'Late (31-120 days)':1,'In Grace Period':1,'Fully Paid':0},inplace=True)\n",
    "data_y=data['loan_status']\n",
    "data_x=data.drop('loan_status',axis=1)\n",
    "data_y.value_counts()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ba7d1c53",
   "metadata": {},
   "source": [
    "# 数据预处理"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6c67892c",
   "metadata": {},
   "source": [
    "## 缺失值处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "e8ba9c1b",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-06-22T01:51:58.220630Z",
     "start_time": "2022-06-22T01:51:58.142583Z"
    }
   },
   "outputs": [],
   "source": [
    "a=data_x.isna().sum(axis=0)\n",
    "a[a>data_x.shape[0]*0.5]\n",
    "data1=data_x.drop(a[a>=data_x.shape[0]*0.5].index,axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "d153aabc",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-06-22T01:51:58.236675Z",
     "start_time": "2022-06-22T01:51:58.222619Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(32892, 105)"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data1.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f525d53d",
   "metadata": {},
   "source": [
    "## 同值化处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "29fa8ade",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-06-22T01:51:58.635763Z",
     "start_time": "2022-06-22T01:51:58.239674Z"
    }
   },
   "outputs": [],
   "source": [
    "for i in data1.columns:\n",
    "    c=data1[i].value_counts()\n",
    "    if c.iloc[0] >=data1.shape[0]*0.9:\n",
    "        data1.drop(i,axis=1,inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "dda57bd4",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-06-22T01:51:58.651254Z",
     "start_time": "2022-06-22T01:51:58.638766Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(32892, 90)"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data1.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ea3deace",
   "metadata": {},
   "source": [
    "## 人工筛选及异常值处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "ff96525a",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-06-22T01:51:58.946750Z",
     "start_time": "2022-06-22T01:51:58.653256Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "float64\n",
      "                               type   size missing  unique   mean_or_top1  \\\n",
      "loan_amnt                   float64  32892   0.00%    1226   15380.449653   \n",
      "funded_amnt                 float64  32892   0.00%    1226   15380.449653   \n",
      "funded_amnt_inv             float64  32892   0.00%    1244   15379.323621   \n",
      "installment                 float64  32892   0.00%    6445     458.705653   \n",
      "annual_inc                  float64  32892   0.00%    3430   87451.821383   \n",
      "dti                         float64  32892   0.28%    4741      20.147935   \n",
      "delinq_2yrs                 float64  32892   0.00%      17       0.241274   \n",
      "inq_last_6mths              float64  32892   0.00%       6       0.567798   \n",
      "open_acc                    float64  32892   0.00%      56      11.769336   \n",
      "pub_rec                     float64  32892   0.00%       5       0.120546   \n",
      "revol_bal                   float64  32892   0.00%   20901   16306.399672   \n",
      "total_acc                   float64  32892   0.00%     106         24.602   \n",
      "tot_coll_amt                float64  32892   0.00%    1796     160.897087   \n",
      "tot_cur_bal                 float64  32892   0.00%   30947  160669.982367   \n",
      "open_acc_6m                 float64  32892   0.00%      11       1.061687   \n",
      "open_act_il                 float64  32892   0.00%      34       2.890703   \n",
      "open_il_12m                 float64  32892   0.00%       7       0.931169   \n",
      "open_il_24m                 float64  32892   0.00%      16       1.991366   \n",
      "mths_since_rcnt_il          float64  32892   2.24%     218        16.3539   \n",
      "total_bal_il                float64  32892   0.00%   25158   40190.328043   \n",
      "il_util                     float64  32892  13.82%     184      71.932616   \n",
      "open_rv_12m                 float64  32892   0.00%      17       1.299739   \n",
      "open_rv_24m                 float64  32892   0.00%      28       2.644321   \n",
      "max_bal_bc                  float64  32892   0.00%   13268    5931.804269   \n",
      "all_util                    float64  32892   0.04%     144      53.997019   \n",
      "total_rev_hi_lim            float64  32892   0.00%    2070   41506.405387   \n",
      "inq_fi                      float64  32892   0.00%      21       1.443238   \n",
      "total_cu_tl                 float64  32892   0.00%      36       1.849751   \n",
      "inq_last_12m                float64  32892   0.00%      32       2.258148   \n",
      "acc_open_past_24mths        float64  32892   0.00%      34       4.945883   \n",
      "avg_cur_bal                 float64  32892   0.02%   20184   15157.092319   \n",
      "bc_open_to_buy              float64  32892   1.54%   21124   17943.160779   \n",
      "bc_util                     float64  32892   1.61%    1071      45.457059   \n",
      "mo_sin_old_il_acct          float64  32892   2.24%     388     125.375793   \n",
      "mo_sin_old_rev_tl_op        float64  32892   0.00%     620     169.171653   \n",
      "mo_sin_rcnt_rev_tl_op       float64  32892   0.00%     177      14.552262   \n",
      "mo_sin_rcnt_tl              float64  32892   0.00%      98       7.402438   \n",
      "mort_acc                    float64  32892   0.00%      18       1.492764   \n",
      "mths_since_recent_bc        float64  32892   1.44%     282       23.93627   \n",
      "mths_since_recent_inq       float64  32892   8.66%      25       6.632917   \n",
      "num_accts_ever_120_pd       float64  32892   0.00%      26       0.484768   \n",
      "num_actv_bc_tl              float64  32892   0.00%      24       3.514806   \n",
      "num_actv_rev_tl             float64  32892   0.00%      32       5.137784   \n",
      "num_bc_sats                 float64  32892   0.00%      35       4.980086   \n",
      "num_bc_tl                   float64  32892   0.00%      48       7.417366   \n",
      "num_il_tl                   float64  32892   0.00%      73       9.496929   \n",
      "num_op_rev_tl               float64  32892   0.00%      49       8.228475   \n",
      "num_rev_accts               float64  32892   0.00%      78      13.410586   \n",
      "num_rev_tl_bal_gt_0         float64  32892   0.00%      32       5.126687   \n",
      "num_sats                    float64  32892   0.00%      56      11.753648   \n",
      "num_tl_op_past_12m          float64  32892   0.00%      20       2.374134   \n",
      "pct_tl_nvr_dlq              float64  32892   0.00%     378      94.842554   \n",
      "percent_bc_gt_75            float64  32892   1.54%     120      29.205027   \n",
      "pub_rec_bankruptcies        float64  32892   0.00%       5       0.120455   \n",
      "tot_hi_cred_lim             float64  32892   0.00%   28470  205306.899976   \n",
      "total_bal_ex_mort           float64  32892   0.00%   28551   56894.815821   \n",
      "total_bc_limit              float64  32892   0.00%    1591    29231.02718   \n",
      "total_il_high_credit_limit  float64  32892   0.00%   23091   50267.487444   \n",
      "\n",
      "                              std_or_top2 min_or_top3 1%_or_top4 10%_or_top5  \\\n",
      "loan_amnt                    10188.369172      1000.0     1200.0      4000.0   \n",
      "funded_amnt                  10188.369172      1000.0     1200.0      4000.0   \n",
      "funded_amnt_inv              10188.662393       850.0     1200.0      4000.0   \n",
      "installment                     294.31708       30.64    40.8258      140.87   \n",
      "annual_inc                  119615.777119         0.0    14000.0     35000.0   \n",
      "dti                             23.544657         0.0       1.34         7.0   \n",
      "delinq_2yrs                      0.778251         0.0        0.0         0.0   \n",
      "inq_last_6mths                   0.830095         0.0        0.0         0.0   \n",
      "open_acc                           6.0029         0.0        3.0         5.0   \n",
      "pub_rec                          0.332167         0.0        0.0         0.0   \n",
      "revol_bal                    22455.180191         0.0        0.0      2015.0   \n",
      "total_acc                       12.812271         2.0        5.0        10.0   \n",
      "tot_coll_amt                  1137.388053         0.0        0.0         0.0   \n",
      "tot_cur_bal                 179200.705182         0.0    2203.91     14374.1   \n",
      "open_acc_6m                      1.210152         0.0        0.0         0.0   \n",
      "open_act_il                      2.925982         0.0        0.0         1.0   \n",
      "open_il_12m                      1.067287         0.0        0.0         0.0   \n",
      "open_il_24m                      1.739727         0.0        0.0         0.0   \n",
      "mths_since_rcnt_il              21.645862         0.0        1.0         3.0   \n",
      "total_bal_il                 48680.673074         0.0        0.0       400.6   \n",
      "il_util                         22.507735         0.0       11.0        42.0   \n",
      "open_rv_12m                      1.515759         0.0        0.0         0.0   \n",
      "open_rv_24m                      2.564352         0.0        0.0         0.0   \n",
      "max_bal_bc                    5868.003366         0.0        0.0       697.1   \n",
      "all_util                        21.128004         0.0        4.0        26.0   \n",
      "total_rev_hi_lim             38420.753031         0.0     2500.0      9900.0   \n",
      "inq_fi                           1.686984         0.0        0.0         0.0   \n",
      "total_cu_tl                      3.059003         0.0        0.0         0.0   \n",
      "inq_last_12m                     2.515871         0.0        0.0         0.0   \n",
      "acc_open_past_24mths             3.368177         0.0        0.0         1.0   \n",
      "avg_cur_bal                  18884.404079         0.0      362.0      1654.0   \n",
      "bc_open_to_buy               22033.898457         0.0        3.0       993.0   \n",
      "bc_util                          29.19651         0.0        0.0         7.2   \n",
      "mo_sin_old_il_acct              56.598789         1.0       11.0        47.0   \n",
      "mo_sin_old_rev_tl_op             96.43461         2.0       30.0        61.0   \n",
      "mo_sin_rcnt_rev_tl_op           17.729273         0.0        0.0         2.0   \n",
      "mo_sin_rcnt_tl                   7.898669         0.0        0.0         1.0   \n",
      "mort_acc                         1.824114         0.0        0.0         0.0   \n",
      "mths_since_recent_bc            32.018755         0.0        1.0         2.0   \n",
      "mths_since_recent_inq            5.738645         0.0        0.0         1.0   \n",
      "num_accts_ever_120_pd            1.402289         0.0        0.0         0.0   \n",
      "num_actv_bc_tl                    2.38671         0.0        0.0         1.0   \n",
      "num_actv_rev_tl                  3.279272         0.0        0.0         2.0   \n",
      "num_bc_sats                      3.266175         0.0        0.0         2.0   \n",
      "num_bc_tl                        4.720411         0.0        1.0         3.0   \n",
      "num_il_tl                        7.819718         0.0        0.0         2.0   \n",
      "num_op_rev_tl                    5.004446         0.0        1.0         3.0   \n",
      "num_rev_accts                    8.163537         2.0        2.0         5.0   \n",
      "num_rev_tl_bal_gt_0              3.266553         0.0        0.0         2.0   \n",
      "num_sats                         6.001041         0.0        3.0         5.0   \n",
      "num_tl_op_past_12m                1.99933         0.0        0.0         0.0   \n",
      "pct_tl_nvr_dlq                   8.736654        19.0       60.0        83.3   \n",
      "percent_bc_gt_75                33.546195         0.0        0.0         0.0   \n",
      "pub_rec_bankruptcies             0.331972         0.0        0.0         0.0   \n",
      "tot_hi_cred_lim             200025.092593         0.0     9200.0     32500.0   \n",
      "total_bal_ex_mort            56440.888648         0.0    1448.73     10781.5   \n",
      "total_bc_limit               27738.536432         0.0        0.0      5100.0   \n",
      "total_il_high_credit_limit   49888.117662         0.0        0.0      1677.3   \n",
      "\n",
      "                           50%_or_bottom5 75%_or_bottom4 90%_or_bottom3  \\\n",
      "loan_amnt                         12750.0        21275.0        31000.0   \n",
      "funded_amnt                       12750.0        21275.0        31000.0   \n",
      "funded_amnt_inv                   12750.0        21275.0        31000.0   \n",
      "installment                        379.45         621.76         897.89   \n",
      "annual_inc                        70000.0       101000.0       150000.0   \n",
      "dti                                 18.09          25.41          32.65   \n",
      "delinq_2yrs                           0.0            0.0            1.0   \n",
      "inq_last_6mths                        0.0            1.0            2.0   \n",
      "open_acc                             11.0           15.0           20.0   \n",
      "pub_rec                               0.0            0.0            1.0   \n",
      "revol_bal                         10562.5        19607.0        34397.8   \n",
      "total_acc                            22.0           32.0           42.0   \n",
      "tot_coll_amt                          0.0            0.0          137.0   \n",
      "tot_cur_bal                       94164.0       242541.5       388228.2   \n",
      "open_acc_6m                           1.0            2.0            3.0   \n",
      "open_act_il                           2.0            4.0            6.0   \n",
      "open_il_12m                           1.0            1.0            2.0   \n",
      "open_il_24m                           2.0            3.0            4.0   \n",
      "mths_since_rcnt_il                   11.0           18.0           34.0   \n",
      "total_bal_il                      26987.0        52502.0        90069.1   \n",
      "il_util                              75.0           87.0           96.0   \n",
      "open_rv_12m                           1.0            2.0            3.0   \n",
      "open_rv_24m                           2.0            4.0            6.0   \n",
      "max_bal_bc                         4451.5         7931.0        12909.6   \n",
      "all_util                             55.0           69.0           80.0   \n",
      "total_rev_hi_lim                  30900.0        53400.0        83500.0   \n",
      "inq_fi                                1.0            2.0            3.0   \n",
      "total_cu_tl                           1.0            2.0            5.0   \n",
      "inq_last_12m                          2.0            3.0            5.0   \n",
      "acc_open_past_24mths                  4.0            7.0            9.0   \n",
      "avg_cur_bal                        8396.0        20918.0        36273.0   \n",
      "bc_open_to_buy                    10463.5       23928.75        43946.5   \n",
      "bc_util                              42.7           69.1           89.2   \n",
      "mo_sin_old_il_acct                  132.0          158.0          188.0   \n",
      "mo_sin_old_rev_tl_op                154.0          216.0          298.0   \n",
      "mo_sin_rcnt_rev_tl_op                 9.0           19.0           35.0   \n",
      "mo_sin_rcnt_tl                        5.0           10.0           16.0   \n",
      "mort_acc                              1.0            2.0            4.0   \n",
      "mths_since_recent_bc                 13.0           30.0           55.0   \n",
      "mths_since_recent_inq                 5.0           10.0           15.0   \n",
      "num_accts_ever_120_pd                 0.0            0.0            1.0   \n",
      "num_actv_bc_tl                        3.0            5.0            7.0   \n",
      "num_actv_rev_tl                       4.0            7.0            9.0   \n",
      "num_bc_sats                           4.0            6.0            9.0   \n",
      "num_bc_tl                             6.0           10.0           13.0   \n",
      "num_il_tl                             7.5           13.0           20.0   \n",
      "num_op_rev_tl                         7.0           11.0           15.0   \n",
      "num_rev_accts                        12.0           17.0           24.0   \n",
      "num_rev_tl_bal_gt_0                   4.0            7.0            9.0   \n",
      "num_sats                             11.0           15.0           20.0   \n",
      "num_tl_op_past_12m                    2.0            3.0            5.0   \n",
      "pct_tl_nvr_dlq                      100.0          100.0          100.0   \n",
      "percent_bc_gt_75                     18.2           50.0          100.0   \n",
      "pub_rec_bankruptcies                  0.0            0.0            1.0   \n",
      "tot_hi_cred_lim                  138451.0      296934.25       457228.8   \n",
      "total_bal_ex_mort                 41601.0       72715.25       117948.8   \n",
      "total_bc_limit                    21000.0        38900.0        63100.0   \n",
      "total_il_high_credit_limit        38071.5       67965.25       108303.8   \n",
      "\n",
      "                           99%_or_bottom2 max_or_bottom1  \n",
      "loan_amnt                         40000.0        40000.0  \n",
      "funded_amnt                       40000.0        40000.0  \n",
      "funded_amnt_inv                   40000.0        40000.0  \n",
      "installment                       1268.46        1604.18  \n",
      "annual_inc                       315000.0      9100000.0  \n",
      "dti                                 59.77          999.0  \n",
      "delinq_2yrs                           4.0           21.0  \n",
      "inq_last_6mths                        3.0            5.0  \n",
      "open_acc                             31.0           64.0  \n",
      "pub_rec                               1.0            4.0  \n",
      "revol_bal                       102487.54       581029.0  \n",
      "total_acc                            64.0          125.0  \n",
      "tot_coll_amt                      3566.72        67137.0  \n",
      "tot_cur_bal                     764061.24      4135549.0  \n",
      "open_acc_6m                           5.0           10.0  \n",
      "open_act_il                          15.0           43.0  \n",
      "open_il_12m                           4.0            6.0  \n",
      "open_il_24m                           8.0           16.0  \n",
      "mths_since_rcnt_il                  117.0          431.0  \n",
      "total_bal_il                     232603.6       970018.0  \n",
      "il_util                             122.0          322.0  \n",
      "open_rv_12m                           6.0           17.0  \n",
      "open_rv_24m                          11.0           33.0  \n",
      "max_bal_bc                       26640.24       179280.0  \n",
      "all_util                            100.0          183.0  \n",
      "total_rev_hi_lim                 184500.0       654500.0  \n",
      "inq_fi                                7.0           26.0  \n",
      "total_cu_tl                          14.0           43.0  \n",
      "inq_last_12m                         11.0           45.0  \n",
      "acc_open_past_24mths                 15.0           34.0  \n",
      "avg_cur_bal                       83944.8       582713.0  \n",
      "bc_open_to_buy                   103370.1       397338.0  \n",
      "bc_util                              99.9          161.1  \n",
      "mo_sin_old_il_acct                 279.45          540.0  \n",
      "mo_sin_old_rev_tl_op                476.0          763.0  \n",
      "mo_sin_rcnt_rev_tl_op                84.0          299.0  \n",
      "mo_sin_rcnt_tl                       37.0          179.0  \n",
      "mort_acc                              7.0           17.0  \n",
      "mths_since_recent_bc                161.0          551.0  \n",
      "mths_since_recent_inq                23.0           24.0  \n",
      "num_accts_ever_120_pd                 7.0           29.0  \n",
      "num_actv_bc_tl                       11.0           26.0  \n",
      "num_actv_rev_tl                      16.0           31.0  \n",
      "num_bc_sats                          16.0           48.0  \n",
      "num_bc_tl                            23.0           72.0  \n",
      "num_il_tl                            37.0           90.0  \n",
      "num_op_rev_tl                        25.0           63.0  \n",
      "num_rev_accts                        40.0          117.0  \n",
      "num_rev_tl_bal_gt_0                  16.0           31.0  \n",
      "num_sats                             31.0           64.0  \n",
      "num_tl_op_past_12m                    9.0           20.0  \n",
      "pct_tl_nvr_dlq                      100.0          100.0  \n",
      "percent_bc_gt_75                    100.0          100.0  \n",
      "pub_rec_bankruptcies                  1.0            4.0  \n",
      "tot_hi_cred_lim                 869504.92      4542835.0  \n",
      "total_bal_ex_mort               270981.87      1048567.0  \n",
      "total_bc_limit                   133309.0       399400.0  \n",
      "total_il_high_credit_limit      233469.31      1029490.0  \n",
      "object\n",
      "                       type   size missing  unique               mean_or_top1  \\\n",
      "term                 object  32892   0.00%       2           36 months:72.14%   \n",
      "emp_length           object  32892   8.11%      11           10+ years:31.25%   \n",
      "home_ownership       object  32892   0.00%       4            MORTGAGE:50.28%   \n",
      "verification_status  object  32892   0.00%       3     Source Verified:42.45%   \n",
      "purpose              object  32892   0.00%      12  debt_consolidation:54.09%   \n",
      "revol_util           object  32892   0.19%    1035                   0%:1.30%   \n",
      "application_type     object  32892   0.00%       2          Individual:86.61%   \n",
      "\n",
      "                             std_or_top2             min_or_top3  \\\n",
      "term                    60 months:27.86%                    None   \n",
      "emp_length               < 1 year:11.41%           2 years:8.66%   \n",
      "home_ownership               RENT:37.21%              OWN:12.47%   \n",
      "verification_status  Not Verified:41.45%         Verified:16.10%   \n",
      "purpose               credit_card:23.99%  home_improvement:6.93%   \n",
      "revol_util                     19%:0.21%             27.1%:0.20%   \n",
      "application_type        Joint App:13.39%                    None   \n",
      "\n",
      "                        1%_or_top4           10%_or_top5 50%_or_bottom5  \\\n",
      "term                          None                  None           None   \n",
      "emp_length           3 years:7.52%          1 year:7.09%  4 years:5.90%   \n",
      "home_ownership           ANY:0.04%                  None           None   \n",
      "verification_status           None                  None           None   \n",
      "purpose                other:6.54%  major_purchase:2.24%      car:1.08%   \n",
      "revol_util              0.1%:0.19%             20%:0.19%   101.5%:0.00%   \n",
      "application_type              None                  None           None   \n",
      "\n",
      "                      75%_or_bottom4          90%_or_bottom3  \\\n",
      "term                            None                    None   \n",
      "emp_length             6 years:4.45%           7 years:3.51%   \n",
      "home_ownership       MORTGAGE:50.28%             RENT:37.21%   \n",
      "verification_status             None  Source Verified:42.45%   \n",
      "purpose               vacation:0.98%            moving:0.83%   \n",
      "revol_util              104.4%:0.00%              129%:0.00%   \n",
      "application_type                None                    None   \n",
      "\n",
      "                           99%_or_bottom2          max_or_bottom1  \n",
      "term                     36 months:72.14%        60 months:27.86%  \n",
      "emp_length                  8 years:3.09%           9 years:2.37%  \n",
      "home_ownership                 OWN:12.47%               ANY:0.04%  \n",
      "verification_status   Not Verified:41.45%         Verified:16.10%  \n",
      "purpose              small_business:0.81%  renewable_energy:0.06%  \n",
      "revol_util                   106.8%:0.00%            102.2%:0.00%  \n",
      "application_type        Individual:86.61%        Joint App:13.39%  \n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Index(['term', 'home_ownership', 'verification_status', 'inq_last_6mths',\n",
       "       'pub_rec', 'application_type', 'open_il_12m', 'pub_rec_bankruptcies'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d=['last_credit_pull_d','last_pymnt_amnt','last_pymnt_d','out_prncp','out_prncp_inv','total_pymnt','total_pymnt_inv',\n",
    "   'total_rec_int','total_rec_prncp','grade','sub_grade','int_rate','id','url','addr_state','emp_title','zip_code','title',\n",
    "   'last_fico_range_high','last_fico_range_low','issue_d','earliest_cr_line','hardship_flag',\n",
    "   'fico_range_low','fico_range_high','issue_d','earliest_cr_line']\n",
    "data1.drop(d,axis=1,inplace=True)\n",
    "data_type=toad.detector.detect(data1)\n",
    "data_type1=data_type.groupby(['type'])\n",
    "for i,j in data_type1:\n",
    "    print(i)\n",
    "    print(j)\n",
    "data1['revol_util']=data1['revol_util'].str.strip('%').astype(float)/100\n",
    "data_type[data_type.unique<10].index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "737c1c5d",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-06-22T01:51:58.963752Z",
     "start_time": "2022-06-22T01:51:58.948750Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(32892, 65)"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data1.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "98476ffe",
   "metadata": {},
   "source": [
    "# 特征分箱"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4525b760",
   "metadata": {},
   "source": [
    "## 区分连续变量与离散变量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "9244f8b6",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-06-22T01:51:58.993787Z",
     "start_time": "2022-06-22T01:51:58.965754Z"
    }
   },
   "outputs": [],
   "source": [
    "discrete=['term','emp_length','home_ownership','verification_status','purpose','application_type','inq_last_6mths', 'pub_rec','open_il_12m',\n",
    "          'pub_rec_bankruptcies']\n",
    "data1_discrete=data1.loc[:,discrete]\n",
    "colu=list(data1.columns)\n",
    "for i in discrete:\n",
    "    colu.remove(i)\n",
    "data1_continuous=data1.loc[:,colu]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "b8ad62bf",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-06-22T01:51:59.009811Z",
     "start_time": "2022-06-22T01:51:58.995797Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(32892, 55)"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data1_continuous.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "51b0ded2",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-06-22T01:51:59.025804Z",
     "start_time": "2022-06-22T01:51:59.011801Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(32892, 10)"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data1_discrete.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "67c88774",
   "metadata": {},
   "outputs": [],
   "source": [
    "data_y.to_csv('data_y.csv',header=True,index=True)\n",
    "data1.to_csv('data1.csv',header=True,index=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "709aeb6a",
   "metadata": {},
   "source": [
    "## 连续变量分箱"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "163759a9",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-06-22T01:52:22.829336Z",
     "start_time": "2022-06-22T01:51:59.026798Z"
    }
   },
   "outputs": [],
   "source": [
    "dict_cont_bin={}\n",
    "for i in data1_continuous.columns:\n",
    "    dict_cont_bin[i],gain_value_save, gain_rate_save=vb.cont_var_bin(data1_continuous[i], data_y, method=1,\n",
    "                                        mmin=3, mmax=12,bin_min_num=20)\n",
    "#mmin:最小分箱数,mmax:最大分箱数,bin_min_num:分箱里最小样本数\n",
    "data2_continuous=pd.DataFrame()\n",
    "for i in dict_cont_bin.keys():\n",
    "    data2_continuous=pd.concat([data2_continuous,vb.cont_var_bin_map(data1_continuous[i],dict_cont_bin[i])],axis=1)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "a8b64dc7",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-06-22T01:52:22.890353Z",
     "start_time": "2022-06-22T01:52:22.831338Z"
    },
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>loan_amnt_BIN</th>\n",
       "      <th>funded_amnt_BIN</th>\n",
       "      <th>funded_amnt_inv_BIN</th>\n",
       "      <th>installment_BIN</th>\n",
       "      <th>annual_inc_BIN</th>\n",
       "      <th>dti_BIN</th>\n",
       "      <th>delinq_2yrs_BIN</th>\n",
       "      <th>open_acc_BIN</th>\n",
       "      <th>revol_bal_BIN</th>\n",
       "      <th>revol_util_BIN</th>\n",
       "      <th>...</th>\n",
       "      <th>num_rev_accts_BIN</th>\n",
       "      <th>num_rev_tl_bal_gt_0_BIN</th>\n",
       "      <th>num_sats_BIN</th>\n",
       "      <th>num_tl_op_past_12m_BIN</th>\n",
       "      <th>pct_tl_nvr_dlq_BIN</th>\n",
       "      <th>percent_bc_gt_75_BIN</th>\n",
       "      <th>tot_hi_cred_lim_BIN</th>\n",
       "      <th>total_bal_ex_mort_BIN</th>\n",
       "      <th>total_bc_limit_BIN</th>\n",
       "      <th>total_il_high_credit_limit_BIN</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>4.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>...</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>7.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>4.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>...</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>...</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>7.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>2.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>...</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>7.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>...</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>143012</th>\n",
       "      <td>4.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>...</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>143021</th>\n",
       "      <td>4.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>...</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>7.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>143024</th>\n",
       "      <td>4.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>...</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>7.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>6.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>143032</th>\n",
       "      <td>4.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>...</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>143033</th>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>...</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>32892 rows × 55 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "        loan_amnt_BIN  funded_amnt_BIN  funded_amnt_inv_BIN  installment_BIN  \\\n",
       "1                 4.0              4.0                  4.0              5.0   \n",
       "6                 4.0              4.0                  4.0              5.0   \n",
       "10                1.0              1.0                  1.0              1.0   \n",
       "13                2.0              2.0                  2.0              3.0   \n",
       "17                3.0              3.0                  3.0              4.0   \n",
       "...               ...              ...                  ...              ...   \n",
       "143012            4.0              4.0                  4.0              5.0   \n",
       "143021            4.0              4.0                  4.0              5.0   \n",
       "143024            4.0              4.0                  4.0              5.0   \n",
       "143032            4.0              4.0                  4.0              5.0   \n",
       "143033            3.0              3.0                  3.0              3.0   \n",
       "\n",
       "        annual_inc_BIN  dti_BIN  delinq_2yrs_BIN  open_acc_BIN  revol_bal_BIN  \\\n",
       "1                  3.0      1.0              1.0           3.0            1.0   \n",
       "6                  4.0      1.0              4.0           3.0            3.0   \n",
       "10                 3.0      1.0              1.0           3.0            1.0   \n",
       "13                 3.0      1.0              1.0           3.0            2.0   \n",
       "17                 3.0      1.0              2.0           3.0            3.0   \n",
       "...                ...      ...              ...           ...            ...   \n",
       "143012             2.0      4.0              1.0           3.0            3.0   \n",
       "143021             1.0      5.0              1.0           3.0            3.0   \n",
       "143024             3.0      4.0              1.0           3.0            1.0   \n",
       "143032             4.0      2.0              1.0           3.0            4.0   \n",
       "143033             2.0      3.0              1.0           4.0            3.0   \n",
       "\n",
       "        revol_util_BIN  ...  num_rev_accts_BIN  num_rev_tl_bal_gt_0_BIN  \\\n",
       "1                  1.0  ...                2.0                      1.0   \n",
       "6                  4.0  ...                3.0                      3.0   \n",
       "10                 2.0  ...                2.0                      1.0   \n",
       "13                 1.0  ...                3.0                      3.0   \n",
       "17                 5.0  ...                2.0                      2.0   \n",
       "...                ...  ...                ...                      ...   \n",
       "143012             5.0  ...                2.0                      3.0   \n",
       "143021             5.0  ...                2.0                      2.0   \n",
       "143024             1.0  ...                2.0                      1.0   \n",
       "143032             4.0  ...                3.0                      3.0   \n",
       "143033             2.0  ...                3.0                      3.0   \n",
       "\n",
       "        num_sats_BIN  num_tl_op_past_12m_BIN  pct_tl_nvr_dlq_BIN  \\\n",
       "1                3.0                     2.0                 7.0   \n",
       "6                3.0                     2.0                 1.0   \n",
       "10               3.0                     2.0                 7.0   \n",
       "13               3.0                     3.0                 7.0   \n",
       "17               3.0                     1.0                 5.0   \n",
       "...              ...                     ...                 ...   \n",
       "143012           3.0                     2.0                 5.0   \n",
       "143021           3.0                     2.0                 7.0   \n",
       "143024           3.0                     2.0                 1.0   \n",
       "143032           3.0                     2.0                 5.0   \n",
       "143033           4.0                     2.0                 1.0   \n",
       "\n",
       "        percent_bc_gt_75_BIN  tot_hi_cred_lim_BIN  total_bal_ex_mort_BIN  \\\n",
       "1                        1.0                  1.0                    2.0   \n",
       "6                        3.0                  1.0                    2.0   \n",
       "10                       3.0                  1.0                    2.0   \n",
       "13                       1.0                  1.0                    2.0   \n",
       "17                       3.0                  1.0                    3.0   \n",
       "...                      ...                  ...                    ...   \n",
       "143012                   4.0                  2.0                    5.0   \n",
       "143021                   3.0                  2.0                    5.0   \n",
       "143024                   5.0                  3.0                    7.0   \n",
       "143032                   2.0                  2.0                    3.0   \n",
       "143033                   1.0                  3.0                    5.0   \n",
       "\n",
       "        total_bc_limit_BIN  total_il_high_credit_limit_BIN  \n",
       "1                      3.0                             2.0  \n",
       "6                      1.0                             2.0  \n",
       "10                     1.0                             1.0  \n",
       "13                     4.0                             1.0  \n",
       "17                     3.0                             2.0  \n",
       "...                    ...                             ...  \n",
       "143012                 3.0                             3.0  \n",
       "143021                 2.0                             5.0  \n",
       "143024                 1.0                             6.0  \n",
       "143032                 5.0                             1.0  \n",
       "143033                 3.0                             5.0  \n",
       "\n",
       "[32892 rows x 55 columns]"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data2_continuous"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "8089c143",
   "metadata": {},
   "outputs": [],
   "source": [
    "dict_cont_bin_save = open('dict_cont_bin.pkl','wb')\n",
    "pickle.dump(dict_cont_bin,dict_cont_bin_save)\n",
    "dict_cont_bin_save.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "81c43598",
   "metadata": {},
   "source": [
    "## 离散变量分箱"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "768c5e29",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-06-22T01:52:23.625525Z",
     "start_time": "2022-06-22T01:52:22.893354Z"
    }
   },
   "outputs": [],
   "source": [
    "dict_disc_bin={}\n",
    "del_key=[]\n",
    "for i in data1_discrete.columns:\n",
    "    dict_disc_bin[i],gain_value_save, gain_rate_save,del_key_1=vb.disc_var_bin(data1_discrete[i],data_y,method=1,\n",
    "                                                                              mmin=3,mmax=12,bin_min_num=20)\n",
    "    if len(del_key_1)>0:\n",
    "        del_key.extend(del_key_1)\n",
    "if len(del_key)>0:\n",
    "    for j in del_key:\n",
    "        del dict_disc_bin[j]\n",
    "data2_discrete=pd.DataFrame()\n",
    "for i in dict_disc_bin.keys():\n",
    "    data2_discrete=pd.concat([data2_discrete,vb.disc_var_bin_map(data1_discrete[i],dict_disc_bin[i])],axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "6ca57cc7",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-06-22T01:52:23.640530Z",
     "start_time": "2022-06-22T01:52:23.627528Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>term_BIN</th>\n",
       "      <th>emp_length_BIN</th>\n",
       "      <th>home_ownership_BIN</th>\n",
       "      <th>verification_status_BIN</th>\n",
       "      <th>purpose_BIN</th>\n",
       "      <th>application_type_BIN</th>\n",
       "      <th>inq_last_6mths_BIN</th>\n",
       "      <th>pub_rec_BIN</th>\n",
       "      <th>open_il_12m_BIN</th>\n",
       "      <th>pub_rec_bankruptcies_BIN</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>1</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>1</td>\n",
       "      <td>5.0</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>1</td>\n",
       "      <td>4.0</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>1</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>143012</th>\n",
       "      <td>1</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>143021</th>\n",
       "      <td>1</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>143024</th>\n",
       "      <td>2</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>143032</th>\n",
       "      <td>2</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>143033</th>\n",
       "      <td>1</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>32892 rows × 10 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "        term_BIN  emp_length_BIN  home_ownership_BIN  verification_status_BIN  \\\n",
       "1              1             1.0                   3                        2   \n",
       "6              1             3.0                   1                        2   \n",
       "10             1             5.0                   3                        3   \n",
       "13             1             4.0                   3                        2   \n",
       "17             1             3.0                   3                        2   \n",
       "...          ...             ...                 ...                      ...   \n",
       "143012         1             3.0                   3                        1   \n",
       "143021         1             4.0                   1                        1   \n",
       "143024         2             3.0                   1                        3   \n",
       "143032         2             1.0                   3                        1   \n",
       "143033         1             3.0                   1                        3   \n",
       "\n",
       "        purpose_BIN  application_type_BIN  inq_last_6mths_BIN  pub_rec_BIN  \\\n",
       "1                 1                     2                   4            2   \n",
       "6                 2                     1                   3            2   \n",
       "10                1                     1                   3            2   \n",
       "13                1                     1                   3            2   \n",
       "17                1                     1                   3            2   \n",
       "...             ...                   ...                 ...          ...   \n",
       "143012            1                     1                   3            1   \n",
       "143021            1                     2                   3            2   \n",
       "143024            1                     1                   3            2   \n",
       "143032            1                     1                   3            2   \n",
       "143033            1                     1                   3            2   \n",
       "\n",
       "        open_il_12m_BIN  pub_rec_bankruptcies_BIN  \n",
       "1                     2                         2  \n",
       "6                     2                         2  \n",
       "10                    1                         2  \n",
       "13                    2                         2  \n",
       "17                    1                         2  \n",
       "...                 ...                       ...  \n",
       "143012                2                         1  \n",
       "143021                2                         2  \n",
       "143024                2                         2  \n",
       "143032                1                         2  \n",
       "143033                1                         2  \n",
       "\n",
       "[32892 rows x 10 columns]"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data2_discrete"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "3fa832ad",
   "metadata": {},
   "outputs": [],
   "source": [
    "dict_disc_bin_save = open('dict_disc_bin.pkl','wb')\n",
    "pickle.dump(dict_disc_bin,dict_disc_bin_save)\n",
    "dict_disc_bin_save.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4ddda3f6",
   "metadata": {},
   "source": [
    "# 相关性和IV筛选"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "c3bc4e18",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-06-22T01:52:24.604292Z",
     "start_time": "2022-06-22T01:52:23.641531Z"
    }
   },
   "outputs": [],
   "source": [
    "data3_continuous=toad.selection.select(data1_continuous,data_y,corr=0.6,iv=0.02)#连续型原始数据做了相关性\n",
    "data3_discrete=toad.selection.select(data2_discrete,data_y,corr=0.6,iv=0.02) #离散型分箱后数据做了相关性\n",
    "#data4_discrete=toad.selection.select(data1_discrete,data_y,corr=0.6,iv=0.02) #原始数据做了相关性\n",
    "b=list(data3_continuous)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "72968154",
   "metadata": {},
   "source": [
    "# WOE编码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "704ee6ec",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-06-22T01:52:24.906382Z",
     "start_time": "2022-06-22T01:52:24.607297Z"
    }
   },
   "outputs": [],
   "source": [
    "c=[]\n",
    "for i in b:\n",
    "    a=i+'_BIN'\n",
    "    c.append(a)\n",
    "data4_continuous=data2_continuous.loc[:,c]\n",
    "data2=pd.concat([data4_continuous,data3_discrete],axis=1)\n",
    "x_train,x_test,y_train,y_test=train_test_split(data2,data_y,test_size=0.2,random_state=20)#设置随机种子，保证每次的训练集和测试集不变\n",
    "data3=pd.concat([data1,data_y],axis=1)\n",
    "data3.rename(columns={'loan_status':'target'},inplace=True)\n",
    "data3.to_csv('data3.csv',header=True,index=True)\n",
    "data_woe_train,dict_woe_map_train,dict_iv_train,var_woe_name_train=ve.woe_encode(x_train,'',list(x_train.columns),y_train,filename='woe',flag='train')\n",
    "data_woe_test,var_woe_name=ve.woe_encode(x_test,'',list(x_test.columns),y_test,filename='woe',flag='test')\n",
    "data_woe_train.index = y_train.index\n",
    "data_woe_test.index = y_test.index\n",
    "data_woe_test1=data_woe_test.drop(columns=['installment_BIN','annual_inc_BIN','dti_BIN','revol_bal_BIN','open_il_24m_BIN','mths_since_rcnt_il_BIN',\n",
    "                            'all_util_BIN','avg_cur_bal_BIN','bc_open_to_buy_BIN','mo_sin_old_il_acct_BIN','mo_sin_old_rev_tl_op_BIN',\n",
    "                            'mort_acc_BIN','num_il_tl_BIN','num_rev_tl_bal_gt_0_BIN','percent_bc_gt_75_BIN',\n",
    "                            'term_BIN','emp_length_BIN','home_ownership_BIN','verification_status_BIN'],axis=1)\n",
    "data_woe_train1=data_woe_train.drop(columns=['installment_BIN','annual_inc_BIN','dti_BIN','revol_bal_BIN','open_il_24m_BIN','mths_since_rcnt_il_BIN',\n",
    "                            'all_util_BIN','avg_cur_bal_BIN','bc_open_to_buy_BIN','mo_sin_old_il_acct_BIN','mo_sin_old_rev_tl_op_BIN',\n",
    "                            'mort_acc_BIN','num_il_tl_BIN','num_rev_tl_bal_gt_0_BIN','percent_bc_gt_75_BIN',\n",
    "                            'term_BIN','emp_length_BIN','home_ownership_BIN','verification_status_BIN'],axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "eda209cb",
   "metadata": {},
   "outputs": [],
   "source": [
    "## 保存WOE规则\n",
    "woe_list = {}\n",
    "woe_save = open('woe_list.pkl','wb')\n",
    "pickle.dump(woe_list,woe_save)\n",
    "woe_save.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "a2780f95",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-06-22T01:52:25.938610Z",
     "start_time": "2022-06-22T01:52:24.908365Z"
    }
   },
   "outputs": [],
   "source": [
    "data_bin_train=pd.concat([x_train,y_train],axis=1)\n",
    "data_bin_test=pd.concat([x_test,y_test],axis=1)\n",
    "data_bin_test.to_csv('data_test.csv',header=True,index=True)#导出分箱数据测试集\n",
    "data_bin_train.to_csv('data_train.csv',header=True,index=True)#导出分箱数据训练集\n",
    "data_train_woe=pd.concat([data_woe_train1,y_train],axis=1)\n",
    "data_test_woe=pd.concat([data_woe_test1,y_test],axis=1)\n",
    "\n",
    "\n",
    "data_test_woe.to_csv('data_test_woe.csv',header=True,index=True)#导出woe编码之后数据测试集\n",
    "data_train_woe.to_csv('data_train_woe.csv',header=True,index=True)#导出woe编码之后数据训练集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "172d08d0",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c087089d",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "59dc9a0a",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6fa3b0e3",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "248.76px"
   },
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
